package com.ziytek.web.citizen.job;

import com.ziytek.web.citizen.config.caffeine.CaffeineUtil;
import com.ziytek.web.citizen.service.CabinetStatusPushService;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * 机柜状态定时更新
 *
 * @author fenghx
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CabinetStatusUpdateJob {
    @Resource
    private CabinetStatusPushService cabinetStatusPushService;

    @PostConstruct
    public void cabinetStatusUpdateInit() {
        log.info("CabinetStatusUpdateJob init success");
        cabinetStatusUpdate();
    }

    public void cabinetStatusUpdate() {
        log.info("CabinetStatusUpdateJob cabinetStatusUpdate");

        String pushData = cabinetStatusPushService.cabinetStatusUpdate();
        if (StringUtils.isBlank(pushData)) {
            return;
        }

        CaffeineUtil.set("status_cache_", pushData, 30);
    }

    @Scheduled(fixedRateString = "20000")
    public void running() {
        log.info("CabinetStatusUpdateJob running");
        cabinetStatusUpdate();
    }

}
